<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="借款额度">
        <el-input-number v-model="integralGrade.borrowAmount" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间开始">
        <el-input-number v-model="integralGrade.integralStart" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间结束">
        <el-input-number v-model="integralGrade.integralEnd" :min="0" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate()"
        >
          提交
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import integralGradeApi from "@/api/integral-grade";
export default {
  data() {
    return {
      integralGrade: {},
      saveBtnDisabled: false,
    };
  },
  created() {
    //获取路径中的id参数
    // console.log(this.$route);
    // console.log(this.$route.params.id);
    //当前页面复用，只有更新时才需要查询积分等级回显
    if (this.$route.params.id) {
      this.getById();
    }
  },
  methods: {
    //根据id查询积分等级
    getById() {
      integralGradeApi.getById(this.$route.params.id).then((r) => {
        this.integralGrade = r.data.item;
      });
    },
    update() {
      integralGradeApi.updateById(this.integralGrade).then((r) => {
        this.$message.success("更新成功");
        //跳转到列表页面
        this.$router.push("/integral-grade/list");
      });
    },
    save() {
      integralGradeApi.save(this.integralGrade).then((r) => {
        this.$message.success("新增成功");
        //跳转到列表页面
        this.$router.push("/integral-grade/list");
      });
    },
    saveOrUpdate() {
      //函数调用时 禁用按钮 防止表单重复提交
      this.saveBtnDisabled = true;
      //判断新增还是更新
      if (this.integralGrade.id) {
        //更新
        this.update();
      } else {
        //新增
        this.save();
      }
    },
  },
};
</script>
